package com.tfswx.jcyw.ruleenginedesigner.api;

import com.tfswx.ruleengine.compute.bean.Gzjyjg;
import com.tfswx.ruleengine.compute.gs.GsUtil;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import com.tfswx.ruleengine.compute.gzjy.RuleEngineUtil;
import com.tfswx.jcyw.ruleenginedesigner.bean.ApiResult;
import com.tfswx.jcyw.ruleenginedesigner.dto.gzgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.GzglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ricky
 * @date 2021/5/11
 */
@RestController
@RequestMapping("/gzgl")
@Api(tags = "规则管理")
public class GzglController {

    @Resource
    private GzglService gzglService;

    @ApiOperation("根据规则分类编码获取规则列表")
    @PostMapping("/queryGzdyListByGzflbm")
    public ApiResult<List<GzdyListByGzflbmQueryOutputDTO>> queryGzdyListByGzflbm(@NonNull @RequestBody @Validated GzdyListByGzflbmQueryInputDTO xmListQueryInputDTO) {
        List<GzdyListByGzflbmQueryOutputDTO> gzdyList = gzglService.queryGzdyListByGzflbm(xmListQueryInputDTO);
        return ApiResult.success(gzdyList);
    }

    @ApiOperation("根据规则编码获取规则定义")
    @PostMapping("/queryGzdyByGzbm")
    public ApiResult<GzdyByGzbmQueryOutputDTO> queryGzdyByGzbm(@NonNull @RequestBody @Validated GzdyByGzbmQueryInputDTO xmListQueryInputDTO) {
        GzdyByGzbmQueryOutputDTO gzdy = gzglService.queryGzdyByGzbm(xmListQueryInputDTO);
        return ApiResult.success(gzdy);
    }

    @ApiOperation("新增规则定义")
    @PostMapping("/addGzdy")
    public ApiResult<String> addGzdy(@NonNull @RequestBody @Validated GzdyAddInputDTO gzdyAddInputDTO) {
        String gzbm = gzglService.addGzdy(gzdyAddInputDTO);
        return ApiResult.success(gzbm);
    }

    @ApiOperation("修改规则定义")
    @PostMapping("/updateGzdy")
    public ApiResult<Boolean> updateGzdy(@NonNull @RequestBody @Validated GzdyUpdateInputDTO gzdyUpdateInputDTO) {
        boolean isSucc = gzglService.updateGzdy(gzdyUpdateInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("修改规则表达式")
    @PostMapping("/updateGzbds")
    public ApiResult<Boolean> updateGzbds(@NonNull @RequestBody @Validated GzbdsUpdateInputDTO gzbdsUpdateInputDTO) {
        boolean isSucc = gzglService.updateGzbds(gzbdsUpdateInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("规则定义排序")
    @PostMapping("/sortGzdy")
    public ApiResult<Boolean> sortGzdy(@NonNull @RequestBody @Validated List<GzdySortInputDTO> sortInputDtoList) {
        boolean isSucc = gzglService.sortGzdy(sortInputDtoList);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("删除规则定义")
    @PostMapping("/deleteGzdy")
    public ApiResult<Boolean> deleteGzdy(@NonNull @RequestBody @Validated GzdyDeleteInputDTO gzdyDeleteInputDTO) {
        boolean isSucc = gzglService.deleteGzdy(gzdyDeleteInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("设置规则启用状态")
    @PostMapping("/setGzdyQyzt")
    public ApiResult<Boolean> setGzdyQyzt(@NonNull @RequestBody @Validated GzdyQyztSetInputDTO gzdyQyztSetInputDTO) {
        boolean isSucc = gzglService.setGzdyQyzt(gzdyQyztSetInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("根据规则编码获取表达式列表")
    @PostMapping("/queryBdsListByGzbm")
    public ApiResult<List<BdsListByGzbmQueryOutputDTO>> queryBdsListByGzbm(@NonNull @RequestBody @Validated BdsListByGzbmQueryInputDTO bdsListByGzbmQueryInputDTO) {
        List<BdsListByGzbmQueryOutputDTO> bdsList = gzglService.queryBdsListByGzbm(bdsListByGzbmQueryInputDTO);
        return ApiResult.success(bdsList);
    }

    @ApiOperation("复制规则")
    @PostMapping("/copyGz")
    public ApiResult<Boolean> copyGz(@NonNull @RequestBody @Validated GzCopyInputDTO gzCopyInputDTO) {
        boolean isSucc = gzglService.copyGz(gzCopyInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("新增表达式")
    @PostMapping("/addBds")
    public ApiResult<String> addBds(@NonNull @RequestBody @Validated BdsAddInputDTO bdsAddInputDTO) {
        String bdsbm = gzglService.addBds(bdsAddInputDTO);
        return ApiResult.success(bdsbm);
    }

    @ApiOperation("修改表达式")
    @PostMapping("/updateBds")
    public ApiResult<Boolean> updateBds(@NonNull @RequestBody @Validated BdsUpdateInputDTO bdsUpdateInputDTO) {
        boolean isSucc = gzglService.updateBds(bdsUpdateInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("删除表达式")
    @PostMapping("/deleteBds")
    public ApiResult<Boolean> deleteBds(@NonNull @RequestBody @Validated BdsDeleteInputDTO bdsDeleteInputDTO) {
        boolean isSucc = gzglService.deleteBds(bdsDeleteInputDTO);
        return ApiResult.success(isSucc);
    }

    @ApiOperation("根据表达式编码获取表达式")
    @PostMapping("/queryBdsByBdsbm")
    public ApiResult<BdsByBdsbmQueryOutputDTO> queryBdsByBdsbm(@NonNull @RequestBody @Validated BdsByBdsbmQueryInputDTO bdsByBdsbmQueryInputDTO) {
        BdsByBdsbmQueryOutputDTO bds = gzglService.queryBdsByBdsbm(bdsByBdsbmQueryInputDTO);
        return ApiResult.success(bds);
    }

    @ApiOperation("规则测试")
    @PostMapping("testRule")
    public ApiResult<Gzjyjg> testRule(@RequestBody @Validated RuleTestInputDTO ruleTestInputDTO) {
        Gzjyjg gzJyJg = gzglService.testRule(ruleTestInputDTO);
        return ApiResult.success(gzJyJg);
    }

    @ApiOperation("获取公式")
    @PostMapping("getGsList")
    public ApiResult<List<GsUtil.GsItem>> getGsList() {
        List<GsUtil.GsItem> gsList = GsUtil.getGsList();
        return ApiResult.success(gsList);
    }

    @ApiOperation("获取方法")
    @PostMapping("getFnList")
    public ApiResult<List<BaseFn>> getFnList() {
        List<BaseFn> fnList = RuleEngineUtil.getFnList().stream().filter(BaseFn::isExport).collect(Collectors.toList());
        return ApiResult.success(fnList);
    }
}
